package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Item;
import csu.web.mypetstore.service.CatalogService;
import csu.web.mypetstore.service.LogService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import csu.web.mypetstore.domain.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ItemFormServlet extends HttpServlet {
    private static final String VIEW_ITEM = "/WEB-INF/jsp/catalog/item.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession httpSession = req.getSession();
        String itemId = req.getParameter("itemId");
        CatalogService catalogService = new CatalogService();
        Item item = catalogService.getItem(itemId);
//        if (httpSession.getAttribute("account") != null) {
//            LogService logService = new LogService();
//            List<Log> logList = (List<Log>) httpSession.getAttribute("logList");
//            logService.updateLog(logList, "View item " + itemId + " details", (Account) httpSession.getAttribute("account"));
//            httpSession.setAttribute("logList", logList);
//        }
        httpSession.setAttribute("item", item);
        req.getRequestDispatcher(VIEW_ITEM).forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
